﻿using DFGK_WMS.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace DFGK_WMS.Context
{
    public class DFGK_DB:DbContext
    {
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Inventory>().HasIndex(s=>s.LocationCode).IsUnique();
            modelBuilder.Entity<Inventory>().HasIndex(s=>s.PalletBarcode).IsUnique();
            base.OnModelCreating(modelBuilder);
        }
        public DFGK_DB(DbContextOptions<DFGK_DB> options) : base(options) { }

        public DbSet<Model.Models.Action> Actions { get; set; }

        public DbSet<Area> Areas { get; set; }

        public DbSet<BarCodeRule> BarCodeRules { get; set; }

        public DbSet<Cell> Cells { get; set;}

        public DbSet<Customer> Customers { get; set; }

        public DbSet<Department> Departments { get; set; }

        public DbSet<Device> Devices { get; set; }

        public DbSet<DictItem> DictItems { get; set; }

        public DbSet<DictType> DictTypes { get; set; }

        public DbSet<ErrorLog> ErrorLogs { get; set; }

        public DbSet<Instruct> Instructs { get; set; }

        public DbSet<InterfaceLog> InterfaceLogs { get; set; }

        public DbSet<Inventory> Inventorys { get; set; }

        public DbSet<InventoryLog> InventoryLogs { get; set; }

        public DbSet<InventoryAdjust> InventoryAdjusts { get; set; }

        public DbSet<InventoryDetail> InventoryDetails { get; set; }

        public DbSet<Log> Logs { get; set; }

        public DbSet<Material> Materials { get; set; }

        public DbSet<MaterialCategory> MaterialCategories { get; set; }

        public DbSet<MaterialGroup> MaterialGroups { get; set; }

        public DbSet<MaterialKey> MaterialKeys { get; set; }

        public DbSet<MaterialPackage> MaterialPackages { get; set; }

        public DbSet<Menu> Menus { get; set; }

        public DbSet<MenuAction> MenuActions { get; set; }

        public DbSet<Pallet> Pallets { get; set; }

        public DbSet<PickingOrder> PickingOrders { get; set;}

        public DbSet<PickingOrderDetail> PickingOrdersDetails { get; set; }

        public DbSet<PutawayOrder> PutawayOrders { get; set; }

        public DbSet<PutawayOrderDetail> PutawayOrdersDetails { get; set;}

        public DbSet<ReceivingBill> ReceivingBills { get; set;}

        public DbSet<ReceivingBillDetail> ReceivingBillsDetails { get;set; }

        public DbSet<ReceivingOrderDetail> ReceivingOrderDetails { get; set;}

        public DbSet<Role> Roles { get; set; }

        public DbSet<RoleMenuAction> RoleMenuActions { get; set; }

        public DbSet<Routeway> Routeways { get; set; }

        public DbSet<Shelf> Shelfs { get; set; }

        public DbSet<ShippingOrderDetail> ShippingOrdersDetails { get; set;}

        public DbSet<ShippingLockCell> ShippingLockCells { get; set;}

        public DbSet<StackingStorage> StackingStorages { get; set; }

        public DbSet<Station> States { get; set; }

        public DbSet<StocktakeResultRecord> StocktakeResultRecords { get; set; }

        public DbSet<StocktakingOrder> StocktakingOrders { get; set; }

        public DbSet<StocktakingOrderDetail> StocktakingOrdersDetails { get; set; }

        public DbSet<Supplier> Suppliers { get; set; }

        public DbSet<TransferBinOrder> TransferBinOrders { get; set; }

        public DbSet<TransferBinOrderDetail> TransferBinOrdersDetails { get; set;}

        public DbSet<User> Users { get; set; }

        public DbSet<UserRole> UserRoles { get; set; }

        public DbSet<Warehouse> WarehouseUsers { get; set;}

        public DbSet<CachePort> CachePorts { get; set; }

        public DbSet<Pack> Packs { get; set; }

        public DbSet<PackageDetail> PackageDetails { get; set; }

        public DbSet<ShippingReplenishment> ShippingReplenishments { get; set; }
        public DbSet<MaterialPackageReceiving> MaterialPackageReceivings { get; set; }
        public DbSet<Employee> Employees { get; set; }
        public DbSet<Stock> Stocks { get; set; }
        public DbSet<Unit> Units { get; set; }
        public DbSet<OtherType> OtherTypes { get; set; }

        public override int SaveChanges()
        {
            //如果状态为Added则为审计字段赋值
            foreach (var item in ChangeTracker.Entries<BaseEntity>())
            {
                if(item.State == EntityState.Added)
                {
                    item.Entity.Id = YitIdHelper.NextId();
                    item.Entity.CreateDate = DateTime.Now;
                }
                else if(item.State == EntityState.Modified)
                {
                    item.Entity.ModifyDate = DateTime.Now;
                    item.Property(x => x.CreateUserId).IsModified = false;
                    item.Property(x => x.CreateUserName).IsModified = false;
                    item.Property(x => x.CreateDate).IsModified = false;
                }
            }        
            return base.SaveChanges();
        }      
    }
}
